package com.www.applyword.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.classic.adapter.CommonRecyclerAdapter;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.adapter.ChoicenessAdapter;
import com.www.applyword.adapter.ChoicenessAdapter2;
import com.www.applyword.adapter.HomeJobAdapter;
import com.www.applyword.adapter.HomeJobAdapter3;
import com.www.applyword.base.BaseFragment;
import com.www.applyword.bean.ChoiceBean;
import com.www.applyword.bean.JobBean;
import com.www.applyword.bean.JobListBean;
import com.www.applyword.bean.ParamsBean;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.ui.home.HomeJobActivity;
import com.www.applyword.utils.getJsonUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * author : huang
 * date   : 2020/7/30 0030 18:46
 * version: 1.0
 */
public class ChoiceNewFragment extends BaseFragment {
    @BindView(R.id.rl_job2)
    RecyclerView rlJob2;
    @BindView(R.id.vs_empty)
    ViewStub vs_empty;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refresh;

    protected View rootView;

    private Fragment mContent;
    private ChoicenessAdapter2 homeJobAdapter;

    private int page = 1;
    private int Rows = 10;


    @Override
    protected int getLayoutResource() {
        return R.layout.choice_fragment;
    }

    public static ChoiceNewFragment newInstance(int i,int y,int z) {
        ChoiceNewFragment  fragment = new ChoiceNewFragment();
        Bundle args = new Bundle();
        args.putInt("id", i);
        args.putInt("type", y);
        args.putInt("classId", z);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        rlJob2.setLayoutManager(new LinearLayoutManager(getActivity()));
        rlJob2.setItemAnimator(new DefaultItemAnimator());
        homeJobAdapter = new ChoicenessAdapter2(getActivity());
        rlJob2.setAdapter(homeJobAdapter);
        initView(true);
        refresh.setOnRefreshListener(refreshLayout -> initView(true));
        refresh.setOnLoadMoreListener(refreshLayout ->initView(false));
    }


    private void initView(boolean isRefresh) {
        if (isRefresh) page = 1;
        int i = getArguments().getInt("id");
        int type2 = getArguments().getInt("type");
        int classId = getArguments().getInt("classId");
        Log.i("choice",i + "");
        Map<String, Object> params = new HashMap<>();
        params.put("pageNum", page + "");
        params.put("classId", classId + "");
        params.put("pageSize", Rows + "");
        ParamsBean paramsBean = new ParamsBean();
        params.put("appKey", paramsBean.getAppKey());
        params.put("townId", paramsBean.getTownId());
        params.put("version", paramsBean.getVersion());
        params.put("versionCode", paramsBean.getVersionCode());
        params.put("channel", paramsBean.getChannel());
        params.put("downloadSource", paramsBean.getDownloadSource());
        params.put("deviceId", paramsBean.getDeviceId());
        params.put("osVersionName", paramsBean.getOsVersionName());
        params.put("timestamp", paramsBean.getTimestamp());
        params.put("sign", paramsBean.getSign());
        params.put("model", paramsBean.getModel());
        params.put("brand", paramsBean.getBrand());
        params.put("product", paramsBean.getProduct());
        params.put("sdkversion", paramsBean.getSdkversion());
        params.put("imei", paramsBean.getImei());
        params.put("androidid", paramsBean.getAndroidid());
        if (i == 0){
            params.put("entryJobCountSort", "0");
            params.put("classLevel", "1");
            params.put("sortRules", "7");
        }


            HttpClient.post(getActivity(), Constants.list, params, new CallBack<ChoiceBean.DataBean>() {
                @Override
                public void onSuccess(ChoiceBean.DataBean jobListBeans) {
                    if (isRefresh) {
                        refresh.finishRefresh(true);
                        if (jobListBeans.getResults().size() == 0) {
                            showEmpty(true, vs_empty, 0);
                        } else {
                            showEmpty(false, vs_empty, 0);
                        }
                        homeJobAdapter.replaceAll(jobListBeans.getResults(), true);
                    } else {
                        if (jobListBeans.getResults().size() > 0) {
                            refresh.finishLoadMore(true);
                            homeJobAdapter.addAll(jobListBeans.getResults());
                        } else {
                            refresh.finishLoadMoreWithNoMoreData();
                        }
                    }
                    page++;
                }
            });


       /* if (i == 2 && type2 == 2){
            HttpClient.post(getActivity(), Constants.list, params, new CallBack<ChoiceBean.DataBean>() {
                @Override
                public void onSuccess(ChoiceBean.DataBean jobListBeans) {
                    homeJobAdapter.replaceAll(jobListBeans.getResults(), true);
                    showEmpty(false, vs_empty, 0);
                }
            });
        }
        if (i == 8 && type2 == 8){
            HttpClient.post(getActivity(), Constants.list, params, new CallBack<ChoiceBean.DataBean>() {
                @Override
                public void onSuccess(ChoiceBean.DataBean jobListBeans) {
                    homeJobAdapter.replaceAll(jobListBeans.getResults(), true);
                    showEmpty(false, vs_empty, 0);
                }
            });
        }*/


        homeJobAdapter.setOnItemClickListener(new CommonRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int i) {
                ChoiceBean.DataBean.ResultsBean resultsBean =  homeJobAdapter.getItem(i);
                JobListBean jobListBean =  new JobListBean();
                jobListBean.setTitle(jobListBean.getTitle());
                //   jobListBean.getClearingForm().setValue(resultsBean.jobTagFormList.get(1).value);
                jobListBean.setJobDate(resultsBean.jobTagFormList.get(0).value);
                jobListBean.setSalary(resultsBean.salary);


                startActivity(new Intent(getActivity(), HomeJobActivity.class).putExtra("id",homeJobAdapter.getItem(i).partJobId)
                        .putExtra("jobListBeans",jobListBean));
            }
        });


       /* if (i == 0) {
            rlJob2.setLayoutManager(new LinearLayoutManager(getActivity()));
            rlJob2.setItemAnimator(new DefaultItemAnimator());
            HomeJobAdapter homeJobAdapter = new HomeJobAdapter(getActivity());
            rlJob2.setAdapter(homeJobAdapter);
            String jsonData = new getJsonUtil().getJson(getActivity(), "job.json");
            List<JobListBean> jobBean = (List<JobListBean>) new Gson().fromJson(jsonData, JobListBean.class);
            homeJobAdapter.replaceAll(jobBean, true);
            homeJobAdapter.setOnItemClickListener(new CommonRecyclerAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int i) {
                    startActivity(new Intent(getActivity(), HomeJobActivity.class).putExtra("id",homeJobAdapter.getItem(i).getPartJobId()));
                }
            });
        }*/


      /*  HttpClient.post(getActivity(), Constants.initList, params, new CallBack<List<JobListBean>>() {
            @Override
            public void onSuccess(List<JobListBean> jobListBeans) {
                homeJobAdapter.replaceAll(jobListBeans, true);
            }
        });*/
        //得到职位
        //   getJobList();
    }
}
